home *** CD-ROM | disk | FTP | other *** search
/ Language/OS - Multiplatform Resource Library / LANGUAGE OS.iso / a_utils / _archvrs / unix / comp430 / atob.c next >
C/C++ Source or Header  |  1990-01-17  |  4KB  |  181 lines

  1. /* atob: version 4.0
  2.  * stream filter to change printable ascii from "btoa" back into 8 bit bytes
  3.  * if bad chars, or Csums do not match: exit(1) [and NO output]
  4.  *
  5.  *  Paul Rutter        Joe Orost
  6.  *  philabs!per        petsd!joe
  7.  */
  8. /*
  9.  *  DjG - Don Glostein
  10.  *  made changes to code for compiling on non-unix compilers. Main changes
  11.  *  were to the long constants. Originally, not all of them were declared
  12.  *  as such and some compilers promoted in undefined ways. Unlink() function
  13.  *  call moved to end. If done while file was open, on some operating systems
  14.  *  this produced an error at best, and lost clusters at worst.
  15.  *  Introduced code for temp_file to be not in the /tmp directory, for non
  16.  *  unix systems. Introduced SETMODE for those operating systems that default
  17.  *  to new line translation (text mode) for stdin/stdout buffered streams.
  18.  *  NOTE: check the SETMODE and TEMPFILE defines. Make sure they are right for
  19.  *  your machine.
  20.  *  NOTE: if this code is compiled with no defines, then it will default to the
  21.  *  unix operating system version.
  22.  */
  23.  
  24.  
  25. #include <stdio.h> /* include for all */
  26.  
  27. #ifdef MSC
  28. #include <fcntl.h>
  29. #include <io.h>
  30. #define SETMODE(x) setmode(fileno((x)),O_BINARY)
  31. #define TEMPFILE "atob.%x"
  32. #endif
  33.  
  34. #ifdef MWC_ATARI
  35. #define SETMODE(x) ((x)->_ff &= ~(_FASCII))
  36. #define TEMPFILE "atob.%x"
  37. #endif
  38. /* following take care of unix systems */
  39. #ifndef TEMPFILE
  40. #define TEMPFILE "/usr/tmp/atob.%x"
  41. #endif
  42. #ifndef SETMODE
  43. #define SETMODE(x)
  44. #endif
  45. #define reg register
  46.  
  47. #define streq(s0, s1)  strcmp(s0, s1) == 0
  48.  
  49. #define times85(x)    ((((((x<<2)+x)<<2)+x)<<2)+x)
  50.  
  51. long int Ceor = 0L;
  52. long int Csum = 0L;
  53. long int Crot = 0L;
  54. long int word = 0L;
  55. int bcount = 0;
  56. FILE *tmp_file= (FILE *)NULL;
  57. char tmp_name[100]= {'\0'};
  58.  
  59. fatal() {
  60.   fprintf(stderr, "bad format or Csum to atob\n");
  61.   if (tmp_file != (FILE *)NULL)
  62.       unlink(tmp_name);
  63.   exit(1);
  64. }
  65.  
  66. #define DE(c) ((c) - '!')
  67.  
  68. decode(c)
  69.   reg c;
  70. {
  71.   if (c == 'z') {
  72.     if (bcount != 0) {
  73.       fatal();
  74.     } else {
  75.       byteout(0);
  76.       byteout(0);
  77.       byteout(0);
  78.       byteout(0);
  79.     }
  80.   } else if ((c >= '!') && (c < ('!' + 85))) {
  81.     if (bcount == 0) {
  82.       word = DE(c);
  83.       ++bcount;
  84.     } else if (bcount < 4) {
  85.       word = times85(word);
  86.       word += DE(c);
  87.       ++bcount;
  88.     } else {
  89.       word = times85(word) + DE(c);
  90.       byteout((int)((word >> 24) & 255));
  91.       byteout((int)((word >> 16) & 255));
  92.       byteout((int)((word >> 8) & 255));
  93.       byteout((int)(word & 255));
  94.       word = 0;
  95.       bcount = 0;
  96.     }
  97.   } else {
  98.     fatal();
  99.   }
  100. }
  101.  
  102.  
  103. byteout(c)
  104.   reg c;
  105. {
  106.   Ceor ^= c;
  107.   Csum += c;
  108.   Csum += 1;
  109.   if ((Crot & 0x80000000)) {
  110.     Crot <<= 1;
  111.     Crot += 1;
  112.   } else {
  113.     Crot <<= 1;
  114.   }
  115.   Crot += c;
  116.   putc(c, tmp_file);
  117. }
  118.  
  119. main(argc, argv)
  120.   char **argv;
  121. {
  122.   reg c;
  123.   reg long int i;
  124.   int ret;
  125.   char buf[100];
  126.   long int n1, n2, oeor, osum, orot;
  127.  
  128.   if (argc != 1) {
  129.     fprintf(stderr,"bad args to %s\n", argv[0]);
  130.     exit(2);
  131.   }
  132.   sprintf(tmp_name,TEMPFILE,getpid());
  133.   tmp_file = fopen(tmp_name, "w+");
  134.   if (tmp_file == NULL) {
  135.     perror("couldn't open temp file");
  136.     fatal();
  137.   }
  138.   else {  /* successful opens, reset mode on text conversion systems */
  139.     SETMODE(stdin);
  140.     SETMODE(tmp_file);
  141.     SETMODE(stdout);
  142.   }
  143.  
  144.   /*search for header line*/
  145.   for (;;) {
  146.     if (fgets(buf, sizeof buf, stdin) == NULL) {
  147.       fatal();
  148.     }
  149.     if (streq(buf, "xbtoa Begin\n")) {
  150.       break;
  151.     }
  152.   }
  153.  
  154.   while ((c = getchar()) != EOF) {
  155.     if (c == '\n') {
  156.       continue;
  157.     } else if (c == 'x') {
  158.       break;
  159.     } else {
  160.       decode(c);
  161.     }
  162.   }
  163.   if((ret= scanf("btoa End N %ld %lx E %lx S %lx R %lx\n",
  164.          &n1, &n2, &oeor, &osum, &orot)) != 5) {
  165.             fprintf(stderr,"\nscanf returned %d",ret);
  166.     fatal();
  167.   }
  168.   if ((n1 != n2) || (oeor != Ceor) || (osum != Csum) || (orot != Crot)) {
  169.     fatal();
  170.   } else {
  171.     /*copy OK tmp file to stdout*/;
  172.     fseek(tmp_file, 0L, 0);
  173.     for (i = n1; --i >= 0;) {
  174.       putchar(getc(tmp_file));
  175.     }
  176.   }
  177.   unlink(tmp_name); /* Make file disappear */
  178.   exit(0);
  179. }
  180.  
  181.